package edu.nju.desserthouse.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import nju.edu.desserthouse.model.Goods;
import edu.nju.desserthouse.dao.GoodsDao;

/**
 * Session Bean implementation class GoodsDao
 */
@Stateless
public class GoodsDaoBean implements GoodsDao{
	
	private static final long serialVersionUID = 1L;

	@PersistenceContext(unitName = "desserthouseJPA")
	protected EntityManager em;
	
	public GoodsDaoBean(){
		
	}
	
	@Override
	public boolean save(Goods goods) {
		try{
			em.persist(goods);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public Goods find(String column, String value) {
		Goods goods = null;
		try{
			Query query = em.createQuery("from Goods g where g." + column + "=" + value);
			goods = (Goods)query.getSingleResult();
			em.clear();
			return goods;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean updateByGoodsId(Goods goods) {
		try{
			em.merge(goods);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public ArrayList<Goods> getGoodsList(String column, String value) {
		try{
			Query query = em.createQuery("from Goods g where g." + column + "=" + value, Goods.class);
			@SuppressWarnings("unchecked")
			List<Goods> goods = query.getResultList();
			if(goods != null){
				ArrayList<Goods> goodsList = new ArrayList<Goods>(goods);
				em.clear();
				return goodsList;
			}else{
				System.out.println("NULL RESULT!!!");
				em.clear();
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean changePrice(String id, double newPrice) {
		Goods goods = null;
		try{
			Query query = em.createQuery("from Goods g where g.id= " + id);
			goods = (Goods) query.getSingleResult();
			if(goods != null){
				goods.setPrice(newPrice);
				save(goods);
				return true;
			}else{
				System.out.println("NULL!!!");
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean decrease(String id, int num) {
		Goods goods = null;
		try{
			Query query = em.createQuery("from Goods g where g.id= " + id);
			goods = (Goods) query.getSingleResult();
			if(goods != null){
				int left = goods.getNum();
				left -= num;
				goods.setNum(left);
				save(goods);
				return true;
			}else{
				System.out.println("NULL!!!");
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean increase(String id, int num) {
		Goods goods = null;
		try{
			Query query = em.createQuery("from Goods g where g.id= " + id);
			goods = (Goods) query.getSingleResult();
			if(goods != null){
				int left = goods.getNum();
				left += num;
				goods.setNum(left);
				save(goods);
				return true;
			}else{
				System.out.println("NULL!!!");
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
}
